import { useReducer } from "react";

//1.定义一个reducer函数
function reducer(state, action) {
  switch (action.type) {
    case 'INT':
      return state + 1
    case 'DES':
      return state - 1
    case 'SET':
      return action.payload
    default:
      return state
  }
}
function App() {
  //2.使用useReducer钩子函数，解构出状态值和修改状态值的方法。useReducer函数第一个参数为把之前定义的reducer函数传过来，第二个参数为状态值的初始值
  const [state, dispatch] = useReducer(reducer, 0)
  return (
    <div className="App">
      <button onClick={() => dispatch({ type: 'DES' })}>-</button>
      {state}
      <button onClick={() => dispatch({ type: 'INT' })}>+</button>
      <button onClick={() => dispatch({ type: 'SET',payload:100 })}>更新</button>
    </div>
  );
}

export default App;
